<?php
App::uses('AppModel', 'Model');
/**
 * Mensagem Model
 *
 * @property Users $Users
 * @property Tarefas $Tarefas
 */
class Mensagem extends AppModel {

	public $useTable = 'mensagens';
	public $primaryKey = 'id_mensagens';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_mensagens' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'users_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'tarefas_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'proxima_msg' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'users_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Tarefa' => array(
			'className' => 'Tarefa',
			'foreignKey' => 'tarefas_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	function afterFind($results){
		foreach($results as &$item){
			if(isset($item['Mensagem']['created']))
				$item['Mensagem']['created_view'] = $this->dataView($item['Mensagem']['created']);
		}
		return $results;
	}
	
	function dataView($data){
		$temp = explode('-', $data);
		$hora = explode(' ', $temp[2]);
		
		return $hora[1].' '.$hora[0].'/'.$temp[1].'/'.$temp[0];
	}
	
	function ultimaMensagem($idTarefa){
		$return = $this->find('first', array('conditions'=>array('tarefas_id'=>$idTarefa, 'proxima_msg'=>0), 'order'=>'id_mensagens ASC'));
		$return = array('Mensagem'=>$return['Mensagem'], 'User'=>$return['User']);
		return $return;
	}
	
	
	
	function salvaMensagemAnterior($idTarefa, $idProxMsg){
		$ultimaMsg = $this->ultimaMensagem($idTarefa);
		if($ultimaMsg['Mensagem']['id_mensagens'] != $idProxMsg){
			$ultimaMsg['Mensagem']['proxima_msg'] = $idProxMsg;
			if(!$this->save($ultimaMsg))
				return false;
		}
		return true;
	}
}
